﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Twinkle.Tweak
{
    public class TList
    {
        public string NumericId { get; set; }

        public string Id { get; set; }

        public string ParentUserId { get; set; }

        public string[] Members { get; set; }

        public static TList CreateByNode(XElement lNode)
        {
            var list = new TList();
            list.NumericId = lNode.Element("id").Value;
            list.Id = lNode.Element("name").Value;
            list.ParentUserId = lNode.Element("user").Element("screen_name").Value;

            return list;
        }

        public void SetUsers(IEnumerable<TUser> members)
        {
            if (members == null) return;
            var memids = from m in members
                         select m.Id;
            var memidarray = memids.ToArray();
            Members = new string[memidarray.Length];
            memidarray.CopyTo(Members, 0);
        }

        public static string GetId(XElement node)
        {
            if (node == null ||  node.Element("id") == null)
                return null;
            return node.Element("id").Value;
        }

        public override string ToString()
        {
            return "@" + ParentUserId + "/" + Id;
        }
    }
}
